/* @(#)GSMultipleThreadSoket.java    
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-18    新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.integration.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import com.hitrust.gs.bean.GSOrderDetailBean;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.integration.GSApepInfoProcess;
import com.hitrust.gs.integration.GSServerListener;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 *@deprecated 
 */
public class GSMultiThreadServerSoket extends ServerSocket{

    private Socket         iClient;

    private BufferedReader in;

    private PrintWriter    iOut;

    private String         iContent;    
    
    private static String         iResult = "0";
    
    private static GSMultiThreadServerSoket iGSMultiThreadServerSocket;
    
    public GSMultiThreadServerSoket(int arg0) throws IOException {
            super(arg0);    
        
    }
    public GSMultiThreadServerSoket() throws IOException{
        
    }
    /**
     * 
     *监听读取客户端发出的讯息并回复
     */
    public void service() {
        try {
            
            iClient = accept();
            GSLogWriter.debug("开始接收---");
            in = new BufferedReader(new InputStreamReader(iClient.getInputStream(),"GB2312"));
            
            iOut = new PrintWriter(iClient.getOutputStream());
        
            // 接收客户端传入内容
            String instr = in.readLine();            
            if(instr != null){                
                GSLogWriter.debug(this, "client input is :" + instr);
                int preLen =0;
                if (iContent == null){
                    preLen = 0;
                    iContent = instr;
                }
                else{
                    preLen = iContent.length();
                    iContent = iContent + instr;
                }
                int idx = instr.toUpperCase().indexOf("&END");                
                if (idx > -1){
                    idx = preLen + idx;
                    GSServerListener.APEP_TO_BUS_CONTENTS.add(iContent.substring(0, idx));
                    if (iContent.length()>idx +4){
                        iContent = iContent.substring(idx + 4);  //跳过"&END"
                    }
                    else{
                        iContent = "";
                    }
                }
                else if (iContent.length() > 16777216){  // 字符串超长, 清空之
                    iContent = "";
                }
            }

            // 此处收到客户端传来的字符串进行处理，根据处理情况返回0、1信息。
//            iResult = process();
        }
        catch (IOException e) {
//            e.printStackTrace();
            GSLogWriter.error(this, e.toString());

        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
        }
        finally {
            //iOut.println(iResult);
            try {
                if (iOut != null) {
                    iOut.close();
                }
                if (in != null) {
                    in.close();
                }
                if (iClient != null) {
                    iClient.close();
                }
            }
            catch (IOException e) {
                GSLogWriter.error(this, e.toString());
            }
        }
    }
}
